package cn.edu.ctbu.kcsj.webapi;

import cn.edu.ctbu.kcsj.domain.Admin;
import cn.edu.ctbu.kcsj.service.AdminService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/webapi/admin")
public class AdminRestController {
    @Autowired
    private AdminService adminService;
    @RequestMapping ("/logout")
    public int logout(HttpServletRequest httprequest){
        HttpSession session=httprequest.getSession();
        session.setAttribute("userid",null);
        session.setAttribute("user",null);
        return 1;
    }
    @GetMapping("/get")
    public String get(HttpServletRequest httprequest){
        HttpSession session=httprequest.getSession();
        Integer ano=(Integer) session.getAttribute("userid");
        Admin admin=adminService.getByAno(ano);
        String aa=admin.getPassword();
        return aa;
    }
    @PutMapping("/save/{password}")
    public Integer save(HttpServletRequest httprequest,@PathVariable String password){
        //读旧数据
        HttpSession session=httprequest.getSession();
        Integer ano=(Integer) session.getAttribute("userid");
        Admin admin=adminService.getByAno(ano);
        admin.setPassword(password);
        adminService.update(admin);
        return 1;
    }
}

